unit oLoadManager;

interface
uses
  Classes, SysUtils, oManager, oProcessItem, oProcessObjectConstants,
  oDataTypeConstants, oProcessObjects, XMLIntf, oSerializer, XMLDoc;



  type
    ELoadManager = class(Exception)
    end;

    ENotValidGeneratorType = class(ELoadManager)
    end;

    TLoadManager = class (TManager)
    private
      function getNode(paPath : string) : TProcessingXML;
    public
      function ProcessObject(paItem: TProcessItem): TProcessItem; override;
      constructor Create; overload;
      constructor Create(paMaster : TManager); overload;
      destructor Destroy; override;
    end;
implementation

uses
  uOSPRand, oGeneratorData, uOSPDyn;

resourcestring eLoadManNotValidType = 'not a valid generator type';

{ TLoadManager }

constructor TLoadManager.Create;
begin

end;

constructor TLoadManager.Create(paMaster: TManager);
begin
  aMaster := paMaster;
end;

destructor TLoadManager.Destroy;
begin

  inherited;
end;

function TLoadManager.getNode(paPath: string): TProcessingXML;
var
  xml : IXMLDocument;
begin
  if not FileExists(paPath) then
    raise ELoadManager.Create('Invalid path');

  xml := TXMLDocument.Create(nil);
  xml.XML := TSerializer.getStrings(paPath);
  xml.Active := true;
  Result := TProcessingXML.Create(xml.DocumentElement);
end;

function TLoadManager.ProcessObject(paItem: TProcessItem): TProcessItem;
var
  path : TString;
  xml : TProcessingXML;
  node,hlpnode: IXMLNode;
  generatorData: TGeneratorData;
  genType: Integer;
  a,b,c,d: Double;
  Multiplier, Constant: Double;
  Randomized: Boolean;
  probs, vals: pDoubleArr;
  num: integer;
begin
  if not(paItem.Data is TProcessingXML) then
    begin
      if paItem.Data is TString then
        path := Tstring(paItem.Data)
      else
        raise ELoadManager.Create('Invalid input, expected TString, got' +
          paItem.Data.ClassName);
    end;


  case paItem.Code of
    PO_LOADMAN_LOAD_DATA : begin
      try
      xml := getNode(path.String_);
      if xml <> nil then
        result := TProcessItem.create(PO_SUCCESS, xml)
      else
        Result := TProcessItem.create(PO_FAILURE, nil);
      except
        on E : ELoadManager do Result := TProcessItem.create(PO_FAILURE, nil);
      end;
    end;

    LOAD_GENERATOR:
    begin
      a := 0;
      b := 0;
      c := 0;
      d := 0;
      Multiplier := 0;
      Constant := 0;
      Randomized := true;
      //nedostal som dobry processObject
      if not(paItem.Data is TProcessingXML) then raise ELoadManager.Create('');
      //najdem si od savemanazera vytvorenu nodeu-u generator a ak nenajdem, tak zapistim
      node := (paItem.Data as TProcessingXML).Node.ChildNodes.FindNode('generator');
      if node = nil then raise ELoadManager.Create('');

      //zistim si, aky typ idem loadovat a zakladne vlastnosti vsetkych generatorov
      if node.HasAttribute('type') then genType := node.Attributes['type'];
      if node.HasAttribute('Multiplier') then Multiplier := node.Attributes['Multiplier'];
      if node.HasAttribute('Constant') then Constant := node.Attributes['Constant'];
      if node.HasAttribute('Randomized') then Randomized := node.Attributes['Randomized'];

      try
      case genType of

        genGamma:
        begin
          hlpnode := node.ChildNodes.FindNode('Mean');
          if hlpnode.HasAttribute('value') then a := hlpnode.Attributes['value'];
          hlpnode := node.ChildNodes.FindNode('Dispersion');
          if hlpnode.HasAttribute('value') then b := hlpnode.Attributes['value'];
          hlpnode := node.ChildNodes.FindNode('MinX');
          if hlpnode.HasAttribute('value') then c := hlpnode.Attributes['value'];
          hlpnode := node.ChildNodes.FindNode('MaxX');
          if hlpnode.HasAttribute('value') then d := hlpnode.Attributes['value'];
          generatorData := TGammaGeneratorData.create(genType,Multiplier,Constant,a,b,c,d,Randomized);
        end;

        genBeta:
        begin
          hlpnode := node.ChildNodes.FindNode('Mean');
          if hlpnode.HasAttribute('value') then a := hlpnode.Attributes['value'];
          hlpnode := node.ChildNodes.FindNode('Dispersion');
          if hlpnode.HasAttribute('value') then b := hlpnode.Attributes['value'];
          hlpnode := node.ChildNodes.FindNode('MinX');
          if hlpnode.HasAttribute('value') then c := hlpnode.Attributes['value'];
          hlpnode := node.ChildNodes.FindNode('MaxX');
          if hlpnode.HasAttribute('value') then d := hlpnode.Attributes['value'];
          generatorData := TBetaGeneratorData.create(genType,Multiplier,Constant,a,b,c,d,Randomized);
        end;

        genBinomial:
        begin
          hlpnode := node.ChildNodes.FindNode('Number');
          if hlpnode.HasAttribute('value') then num := hlpnode.Attributes['value'];
          hlpnode := node.ChildNodes.FindNode('Probab');
          if hlpnode.HasAttribute('value') then b := hlpnode.Attributes['value'];
          generatorData := TBinomialGeneratorData.create(genType,num,Multiplier,Constant,b,Randomized);
        end;

        genDiscrete:
        begin
          hlpnode := node.ChildNodes.FindNode('Probs');
          //if hlpnode.HasAttribute('value') then probs := hlpnode.Attributes['value'];
          hlpnode := node.ChildNodes.FindNode('Values');
          //if hlpnode.HasAttribute('value') then vals := hlpnode.Attributes['value'];
          generatorData := TDiscreteGeneratorData.create(genType,Multiplier,Constant,probs,vals,Randomized);
        end;

        genErlang:
        begin
          hlpnode := node.ChildNodes.FindNode('Mean');
          if hlpnode.HasAttribute('value') then a := hlpnode.Attributes['value'];
          hlpnode := node.ChildNodes.FindNode('Dispersion');
          if hlpnode.HasAttribute('value') then b := hlpnode.Attributes['value'];
          generatorData := TErlangGeneratorData.create(genType,Multiplier,Constant,a,b,Randomized);
        end;

        genExponential:
        begin
          hlpnode := node.ChildNodes.FindNode('Mean');
          if hlpnode.HasAttribute('value') then a := hlpnode.Attributes['value'];
          generatorData := TExponentialGeneratorData.create(genType,Multiplier,Constant,a,Randomized);
        end;

        genLinear:
        begin
          hlpnode := node.ChildNodes.FindNode('Probs');
          //if hlpnode.HasAttribute('value') then probs := hlpnode.Attributes['value'];
          hlpnode := node.ChildNodes.FindNode('Values');
          //if hlpnode.HasAttribute('value') then vals := hlpnode.Attributes['value'];
          generatorData := TLinearGeneratorData.create(genType,Multiplier,Constant,probs,vals,Randomized);
        end;

        genNormal:
        begin
          hlpnode := node.ChildNodes.FindNode('Mean');
          if hlpnode.HasAttribute('value') then a := hlpnode.Attributes['value'];
          hlpnode := node.ChildNodes.FindNode('Dispersion');
          if hlpnode.HasAttribute('value') then b := hlpnode.Attributes['value'];
          generatorData := TNormalGeneratorData.create(genType,Multiplier,Constant,a,b,Randomized);
        end;

        genPoisson:
        begin
          hlpnode := node.ChildNodes.FindNode('Mean');
          if hlpnode.HasAttribute('value') then a := hlpnode.Attributes['value'];
          generatorData := TPoissonGeneratorData.create(genType,Multiplier,Constant,a,Randomized);
        end;

        genUniform:
        begin
          hlpnode := node.ChildNodes.FindNode('From');
          if hlpnode.HasAttribute('value') then a := hlpnode.Attributes['value'];
          hlpnode := node.ChildNodes.FindNode('To');
          if hlpnode.HasAttribute('value') then b := hlpnode.Attributes['value'];
          generatorData := TUniformGeneratorData.create(genType,Multiplier,Constant,a,b,Randomized);
        end;

        else raise ENotValidGeneratorType.Create(eLoadManNotValidType);
      end;
        except
          on E:ENotValidGeneratorType do raise ELoadManager.Create(eLoadManNotValidType);
          on E:ELoadManager do result := TProcessItem.create(PO_FAILURE,nil);
        //else ak nic nenajde, tak raise-ni vynimku, ze nepozna kod (pozri sa, ako sa to nazyva v inych manazeroch, treba pouzit resourcestringy atd)

      end;

      paItem.Free;
      result := TProcessItem.create(PO_SUCCESS,TProcessingGeneratorData.create(generatorData));

      {
        if genType = 'uniform' then
              begin
                hlpnode := node.ChildNodes.FindNode('From');
                if hlpnode.HasAttribute('value') then a := hlpnode.Attributes['value'];
                hlpnode := node.ChildNodes.FindNode('To');
                if hlpnode.HasAttribute('value') then b := hlpnode.Attributes['value'];
                generator := tUniform.Create(a,b,true);
              end;
              if genType = 'poisson' then
              begin
                hlpnode := node.ChildNodes.FindNode('Mean');
                if hlpnode.HasAttribute('value') then a := hlpnode.Attributes['value'];
                generator := tPoisson.Create(a,true);
              end;
              if genType = 'normal' then
              begin
                hlpnode := node.ChildNodes.FindNode('Mean');
                if hlpnode.HasAttribute('value') then a := hlpnode.Attributes['value'];
                hlpnode := node.ChildNodes.FindNode('Dispersion');
                if hlpnode.HasAttribute('value') then b := hlpnode.Attributes['value'];
                generator := tNormal.Create(a,b,true);
              end;
              if genType = 'linear' then
              begin
                hlpnode := node.ChildNodes.FindNode('Probs');
                //if hlpnode.HasAttribute('value') then probs := hlpnode.Attributes['value'];
                hlpnode := node.ChildNodes.FindNode('Values');
                //if hlpnode.HasAttribute('value') then vals := hlpnode.Attributes['value'];
                //generator := tLinear.Create(probs,vals,true);
                generator := nil;
              end;
              if genType = 'gama' then
              begin
                hlpnode := node.ChildNodes.FindNode('Mean');
                if hlpnode.HasAttribute('value') then a := hlpnode.Attributes['value'];
                hlpnode := node.ChildNodes.FindNode('Dispersion');
                if hlpnode.HasAttribute('value') then b := hlpnode.Attributes['value'];
                hlpnode := node.ChildNodes.FindNode('MinX');
                if hlpnode.HasAttribute('value') then c := hlpnode.Attributes['value'];
                hlpnode := node.ChildNodes.FindNode('MaxX');
                if hlpnode.HasAttribute('value') then d := hlpnode.Attributes['value'];
                generator := tGama.Create(a,b,c,d,true);
              end;
              if genType = 'exponential' then
              begin
                hlpnode := node.ChildNodes.FindNode('Mean');
                if hlpnode.HasAttribute('value') then a := hlpnode.Attributes['value'];
                generator := tExponential.Create(a,true);
              end;
              if genType = 'erlang' then
              begin
                hlpnode := node.ChildNodes.FindNode('Mean');
                if hlpnode.HasAttribute('value') then a := hlpnode.Attributes['value'];
                hlpnode := node.ChildNodes.FindNode('Dispersion');
                if hlpnode.HasAttribute('value') then b := hlpnode.Attributes['value'];
                generator := tErlang.Create(a,b,true);
              end;
              if genType = 'discrete' then
              begin
                hlpnode := node.ChildNodes.FindNode('Probs');
                //if hlpnode.HasAttribute('value') then probs := hlpnode.Attributes['value'];
                hlpnode := node.ChildNodes.FindNode('Values');
                //if hlpnode.HasAttribute('value') then vals := hlpnode.Attributes['value'];
                //generator := tDiscrete.Create(probs,vals,true);
                generator := nil;
              end;
              if genType = 'binomial' then
              begin
                hlpnode := node.ChildNodes.FindNode('Mean');
                if hlpnode.HasAttribute('value') then num := hlpnode.Attributes['value'];
                hlpnode := node.ChildNodes.FindNode('Dispersion');
                if hlpnode.HasAttribute('value') then b := hlpnode.Attributes['value'];
                generator := tBinomial.Create(num,b,true);
              end;
              if genType = 'beta' then
              begin
                hlpnode := node.ChildNodes.FindNode('Mean');
                if hlpnode.HasAttribute('value') then a := hlpnode.Attributes['value'];
                hlpnode := node.ChildNodes.FindNode('Dispersion');
                if hlpnode.HasAttribute('value') then b := hlpnode.Attributes['value'];
                hlpnode := node.ChildNodes.FindNode('MinX');
                if hlpnode.HasAttribute('value') then c := hlpnode.Attributes['value'];
                hlpnode := node.ChildNodes.FindNode('MaxX');
                if hlpnode.HasAttribute('value') then d := hlpnode.Attributes['value'];
                generator := tBeta.Create(a,b,c,d,true);
              end;
      }
    end;  //load generator

  else
    //prerobit na vynimku
    Result := TProcessItem.create(PO_FAILURE, NIL);

  end; //case
end; //processobject

end.
